package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dingtao.common.bean.TuiguangDetail;
import com.dingtao.common.bean.VisMe;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.TuiguangDetailPresenter;
import com.example.peibei.ui.adapter.TuiguangDetailAdapter;
import com.example.peibei.ui.adapter.TuiguangItemAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TuiguangDetailActivity extends WDActivity {
    @BindView(R.id.tv_username)
    TextView tv_username;
    @BindView(R.id.tv_sacle)
    TextView tv_sacle;
    @BindView(R.id.tv_people)
    TextView tv_people;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_last)
    TextView tv_last;
    @BindView(R.id.tv_next)
    TextView tv_next;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.rv_item)
    RecyclerView rv_item;
    @BindView(R.id.ll_item)
    LinearLayout ll_item;

    private TuiguangDetailPresenter tuiguangDetailPresenter;
    List<TuiguangDetail.RecordsDTO> list = new ArrayList<>();
    List<String> items = new ArrayList<>();
    private TuiguangDetailAdapter tuiguangDetailAdapter;
    private int index = 1;
    private int max = 1;
    int VIEW_COUNT = 7;
    private SPUtils spUtils;
    private String token;
    private TuiguangItemAdapter tuiguangItemAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tuiguang_detail;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(TuiguangDetailActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        int personCount = extras.getInt("personCount");
        int serviceCount = extras.getInt("serviceCount");
        double profitMoney = extras.getDouble("profitMoney");
        String scale = extras.getString("scale");
        String nickName = extras.getString("nickName");
        tv_sacle.setText(scale + "%");
        tv_people.setText(serviceCount + "");
        tv_money.setText(profitMoney + "");
        tv_username.setText(nickName + "");
        order_list.setEmptyView(empty_view);
        order_list.setLayoutManager(new LinearLayoutManager(this));
        tuiguangDetailAdapter = new TuiguangDetailAdapter(list);
        order_list.setAdapter(tuiguangDetailAdapter);
        tuiguangDetailPresenter = new TuiguangDetailPresenter(new OrderCall());
        tuiguangDetailPresenter.reqeust("Bearer " + token, index);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rv_item.setLayoutManager(linearLayoutManager);
        tuiguangItemAdapter = new TuiguangItemAdapter(items);
        rv_item.setAdapter(tuiguangItemAdapter);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.tv_last)
    public void up() {
        index--;
        tuiguangDetailPresenter.reqeust("Bearer " + token, index);
        checkButton();
    }

    @OnClick(R.id.tv_next)
    public void next() {
        index++;
        tuiguangDetailPresenter.reqeust("Bearer " + token, index);
        checkButton();
    }

    public void checkButton() {
        // 索引值小于等于0，表示不能向前翻页了，以经到了第一页了。
        // 将向前翻页的按钮设为不可用。
        if (index <= 1) {
            tv_last.setEnabled(false);
        } else {
            tv_last.setEnabled(true);
        }
        // 值的长度减去前几页的长度，剩下的就是这一页的长度，如果这一页的长度比View_Count小，表示这是最后的一页了，后面在没有了。
        // 将向后翻页的按钮设为不可用。
        if (index < max) {
            tv_next.setEnabled(true);
        }// 否则将2个按钮都设为可用的。
        else {
            tv_next.setEnabled(false);
        }
    }

    class OrderCall implements DataCall<TuiguangDetail> {
        @Override
        public void success(TuiguangDetail data, Object... args) {
            //添加列表并刷新
//            if (tuiguangDetailPresenter.getPage() == 1) {
            list.clear();
//            }
            tuiguangDetailAdapter.setNewInstance(data.getRecords());
            tuiguangDetailAdapter.notifyDataSetChanged();

            items.clear();
            max = data.getTotal() / 7 + 1;
            if (max <= 1) {
                tv_next.setEnabled(false);
            } else {
                tv_next.setEnabled(true);
            }
            Log.i("answer", "当前有几页：" + max);
            if (data.getTotal() > 0) {
                ll_item.setVisibility(View.VISIBLE);
            } else {
                ll_item.setVisibility(View.GONE);
            }
            for (int i = 0; i < max; i++) {
                items.add(i + 1 + "");
            }
            tuiguangItemAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
